package com.xinghou.platform.dao.main.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.mongodb.WriteResult;
import com.xinghou.platform.model.main.SharedCardLocationGeo;

/**
 * 
 * @author xuzhen
 *
 */
@Service
public class SharedCardLocationMONGOImpl  {/*

	@Resource
	private MongoTemplate mongoTemplate;
	
	*//**
	 * 创建mongo数据表
	 *//*
	public void createCollection() {  
        if (!this.mongoTemplate.collectionExists(SharedCardLocationGeo.class)) {  
            this.mongoTemplate.createCollection(SharedCardLocationGeo.class);  
        }
    }

	
	*//*public void insert(SharedCardLocationGeo sharedCardLocationGeo) {
		mongoTemplate.insert(sharedCardLocationGeo);
		
	}*//*

	
	*//*public SharedCardLocationGeo findOne(int id) {
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), SharedCardLocationGeo.class);
	}*//*

	
	*//*public SharedCardLocationGeo findOnebyshareid(int cardId) {
		return mongoTemplate.findOne(new Query(Criteria.where("cardId").is(cardId)), SharedCardLocationGeo.class);
	}
*//*
	
	*//*public List<SharedCardLocationGeo> findAll() {
		return mongoTemplate.findAll(SharedCardLocationGeo.class);
	}*//*

	
	public List<SharedCardLocationGeo> nearSphere(Point point,int limit,int skip,double maxDistance,int type) {

		Query query = new Query();
		query.addCriteria(Criteria.where("pos").nearSphere(point).maxDistance(maxDistance));
		if(type == 1){
			query.addCriteria(Criteria.where("type").is(1));
		}else if(type == 2){
			query.addCriteria(Criteria.where("type").is(2));
		}
		query.addCriteria(Criteria.where("status").is(1));
		
		return mongoTemplate.find(query.limit(limit).skip(skip), SharedCardLocationGeo.class);
	}

	
	*//*public List<SharedCardLocationGeo> near(Point point, String orderFeild,
			int limit, double maxDistance) {
		Criteria criteria = Criteria.where("pos").near(point).maxDistance(maxDistance);

		Query query = new Query(criteria);
		if(orderFeild != null){
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		}
		return mongoTemplate.find(query.limit(limit), SharedCardLocationGeo.class);
	}*//*

	
	*//*public List<SharedCardLocationGeo> withinSphere(Point center, String orderFeild,
			int limit, double radius) {
		Circle circle = new Circle(center, radius);
		Criteria criteria = Criteria.where("pos").withinSphere(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), SharedCardLocationGeo.class);
	}*//*

	
	*//*public List<SharedCardLocationGeo> within(Point center, String orderFeild,
			int limit, double radius) {
		Sphere circle = new Sphere(center, radius);
		Criteria criteria = Criteria.where("pos").within(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), SharedCardLocationGeo.class);
	}*//*

	
	public boolean update(SharedCardLocationGeo sharedCardLocationGeo) {
		boolean result = false;
		
		Query query = new Query();  
        query.addCriteria(new Criteria("cardId").is(sharedCardLocationGeo.getCardId()));
        Update update = new Update();  
        update.set("pos", sharedCardLocationGeo.getPos());
        update.set("storeId", sharedCardLocationGeo.getStoreId());
        update.set("status", sharedCardLocationGeo.getStatus());
        update.set("createtime", sharedCardLocationGeo.getCreatetime());
        WriteResult writeResult = this.mongoTemplate.updateFirst(query, update, SharedCardLocationGeo.class);  
        
		if (!writeResult.wasAcknowledged()) {
            result = true;
        }
		
		return result;
	}  

	public boolean delete(SharedCardLocationGeo sharedCardLocationGeo) {
		boolean falg = false;
		Query query = new Query();  
        query.addCriteria(new Criteria("cardId").is(sharedCardLocationGeo.getCardId()));
        WriteResult writeResult = this.mongoTemplate.remove(query, SharedCardLocationGeo.class);
        if (!writeResult.wasAcknowledged()) {
            falg = true;
        }
        return falg;
	}
*/
}
